package ee.serge.client.content;

import java.util.List;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import ee.serge.client.Move;
import ee.serge.shared.Loan;
import ee.serge.util.Content;
import ee.serge.util.Headers;
import ee.serge.util.RatingPanel;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BTable;

public class FullLoansContent extends Content {

   private static final String NAME = "all_loans";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData() {
      URLData data = new URLData();
      data.setName(NAME);
      return data;
   }
   
   public static void show() {
      Move.get().setContent(new FullLoansContent());
   }
   
   public FullLoansContent() {
      setHeader("All loans", "Full list of all offers");
      if (LoansRequester.get().isLoading()) {
         LoansRequester.get().addValueChangeHandler(new ValueChangeHandler<List<Loan>>() {
            @Override
            public void onValueChange(ValueChangeEvent<List<Loan>> event) {
               buildForm();
            }
         });
      } else {
         buildForm();
      }
      
   }
   
   private void buildForm() {
      add(buildTable(LoansRequester.get().getLoans(), 0, 0));
   }
   
   private static Widget buildTable(List<Loan> loans, int loanSum, int loanPeriod) {
   	BTable table = new BTable(loans.size(), 4, BTable.DEFAULT);
      table.setWidth("100%");
      table.addStyleName("loan-table");
      for (int i = 0; i < loans.size(); i++) {
         Loan loan = LoansRequester.get().getLoans().get(i);
         table.getCellFormatter().addStyleName(i, 1, "full-width");
         table.setWidget(i, 0, buildFirstContent(loan));
         table.setWidget(i, 1, buildSecondContent(loan));
         table.setWidget(i, 2, buildThirdContent(loan, loanSum, loanPeriod));
         table.setWidget(i, 3, buildĹastContent(loan));
         
      }
      return table;
    }

	private static Widget buildThirdContent(Loan loan, int loanSum, int loanPeriod) {
		Grid grid = new Grid(2, 1);
		grid.setWidget(0, 0, new HTML("APR: <b>" + loan.getAPR() + "%</b>"));
		grid.setWidget(1, 0, new HTML("Transfer fee: <b>" + loan.getTransferFee() + "%</b>"));
		grid.addStyleName("left");
		return grid;
	}

	private static Widget buildSecondContent(Loan loan) {
		Grid grid = new Grid(2, 1);
		grid.setWidget(0, 0, Headers.h4(loan.getName()));
		grid.setWidget(1, 0, new Label(loan.getComment()));
		grid.getCellFormatter().addStyleName(0, 0, "header");
		grid.getCellFormatter().addStyleName(1, 0, "full-height nopadding");
	   return grid;
   }

	private static Image buildFirstContent(Loan loan) {
	   return new Image(loan.getImageLink());
   }   
   
   private static Widget buildĹastContent(Loan loan) {
      FlowPanel flow = new FlowPanel();
      RatingPanel ratingPanel = new RatingPanel((float)(loan.getTotalRating()) / loan.getFeedbackCount(), false);
      flow.add(ratingPanel);
      Hyperlink hyperlink = new Hyperlink("Read more..", LoanDetailsContent.getURLData(loan.getId()).getHref());
      flow.add(hyperlink/*label = new ClickLabel("Read more..", new ReadMoreHandler(loan.getId()))*/);
      //DOM.appendChild(label.getElement(), DOM.createElement("br"));
      flow.add(new Anchor("Appy now", loan.getLink()));
      return flow;
   }
}
